//
//  ActivityJoinTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ActivityJoinTableViewCell.h"

@interface ActivityJoinTableViewCell ()
/**  图片   */
@property (strong, nonatomic) UIImageView  *actImageView;
/**  标题  */
@property (strong, nonatomic) UILabel  *actLabel;
/**  时间图片  */
@property (strong, nonatomic) UIImageView  *timeImageVIew;
/**  时间  */
@property (strong, nonatomic) UILabel  *timeLabel;
/**  人数  */
@property (strong, nonatomic) UILabel  *actNumberLabel;
/**  地点  */
@property (strong, nonatomic) UILabel  *addressLabel;
/**  地点图片  */
@property (strong, nonatomic) UIImageView  *addressImageView;
/**  活动状态  */
@property (strong, nonatomic) UILabel  *statusLabel;
/**  活动类型  */
@property (strong, nonatomic) UILabel  *typeLabel;
/**  判断是否审核  */
@property (strong, nonatomic) UILabel  *checkLabel;


@end


/**  120  */
@implementation ActivityJoinTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
        
        
        TenGrayShadowView *under = [TenGrayShadowView new];
        under.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:under];
        under.sd_layout.bottomEqualToView(self).heightIs(10 * [UIScreen zj_screenHScale]).widthIs([UIScreen zj_screenWith]).leftEqualToView(self);
    }
    return self;
}





#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.actImageView];
    [self.actImageView addSubview:self.checkLabel];
    [self addSubview:self.addressImageView];
    [self addSubview:self.addressLabel];
    [self addSubview:self.statusLabel];
    [self addSubview:self.timeImageVIew];
    [self addSubview:self.timeLabel];
    [self addSubview:self.actLabel];
    [self addSubview:self.actNumberLabel];
    
    
    self.actImageView.sd_layout
    .topSpaceToView(self,10 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self,12 * [UIScreen zj_screenWScale])
    .heightIs(90 * [UIScreen zj_screenHScale])
    .widthIs(130 * [UIScreen zj_screenWScale]);
    /**  审核  */
    self.checkLabel.sd_layout
    .topEqualToView(self.actImageView)
    .leftEqualToView(self.actImageView)
    .heightIs(38 * [UIScreen zj_screenHScale])
    .widthIs(23 * [UIScreen zj_screenWScale]);
    [self.checkLabel setRadius:10 byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];

    
    
    /**  加入  */
    self.statusLabel.sd_layout
    .bottomEqualToView(self.actImageView)
    .rightSpaceToView(self,5 * [UIScreen zj_screenWScale])
    .heightIs(27 * [UIScreen zj_screenHScale])
    .widthIs(50 * [UIScreen zj_screenWScale]);
    
    self.typeLabel.sd_layout
    .topEqualToView(self.actImageView)
    .leftSpaceToView(self.actImageView,0 * [UIScreen zj_screenWScale])
    .heightIs(20 * [UIScreen zj_screenHScale])
    .widthIs(self.actImageView.width);
    
    
    self.addressImageView.sd_layout
    .bottomEqualToView(self.actImageView)
    .leftSpaceToView(self.actImageView,10 * [UIScreen zj_screenWScale])
    .heightIs(12 * [UIScreen zj_screenHScale])
    .widthIs(12 * [UIScreen zj_screenWScale]);
    
    self.addressLabel.sd_layout
    .bottomEqualToView(self.actImageView)
    .leftSpaceToView(self.addressImageView,10 * [UIScreen zj_screenWScale])
    .heightIs(12 * [UIScreen zj_screenHScale])
    .rightSpaceToView(self.statusLabel, 5 * [UIScreen zj_screenWScale]);
    
   
    
    self.timeImageVIew.sd_layout
    .bottomSpaceToView(self.addressImageView,8 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.addressImageView)
    .heightIs(12 * [UIScreen zj_screenHScale])
    .widthIs(12 * [UIScreen zj_screenWScale]);
    
    self.timeLabel.sd_layout
    .bottomSpaceToView(self.addressImageView,8 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.addressLabel)
    .heightIs(12 * [UIScreen zj_screenHScale])
    .widthRatioToView(self.addressLabel, 1);
    
    
    
    self.actLabel.sd_layout
    .topEqualToView(self.actImageView)
    .leftEqualToView(self.addressImageView)
    .heightIs([NSString heightWithTextFont:self.actLabel.font])
    .widthIs([UIScreen zj_screenWith] - 172 * [UIScreen zj_screenWScale]);
    
    self.actNumberLabel.sd_layout
    .topSpaceToView(self.actLabel, 5 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.actLabel)
    .heightIs(13 * [UIScreen zj_screenHScale])
    .widthIs(150 * [UIScreen zj_screenWScale]);
    
}


#pragma mark - 私有方法
- (void)setModel:(ActivityJoinDetailModel *)model{
    _model = model;
    NSString *imageString;
    if (model.COVER_JSON.count == 0) {
        imageString = @"34123";
    }else{
        imageString = model.COVER_JSON[0][@"file_path"];
    }
    [self.actImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,imageString]] placeholderImage:PlaceholderImage];
    
    if (model.ADDRESS.length == 0) {
        self.addressLabel.text = @"暂无";
    }else{
        self.addressLabel.text = model.ADDRESS;
    }
    if (model.START_TIME.length == 0) {
        self.timeLabel.text = @"待定";
    }else{
        NSDateFormatter *dateformatter = [NSDateFormatter new];
        dateformatter.dateFormat = @"MM月dd日 HH:mm";
        NSArray *array = [model.START_TIME componentsSeparatedByString:@"."];
        self.timeLabel.text = [dateformatter stringFromDate: [NSDateFormatter dateTimeWithString:array[0]]];
        
    }
    
    self.actLabel.text = model.NAME;
    if (self.isTitleTwo) {//显示两行
        self.actNumberLabel.hidden = YES;
        self.actLabel.numberOfLines = 2;
        self.actLabel.sd_layout
        .topEqualToView(self.actImageView)
        .leftEqualToView(self.addressImageView)
        .widthIs([UIScreen zj_screenWith] - 172 * [UIScreen zj_screenWScale])
        .heightIs([self.actLabel getLableSizeWithMaxWidth:self.actLabel.width].height);
    }else{
        self.actLabel.sd_layout
        .topEqualToView(self.actImageView)
        .leftEqualToView(self.addressImageView)
        .heightIs([NSString heightWithTextFont:self.actLabel.font])
        .widthIs([UIScreen zj_screenWith] - 172 * [UIScreen zj_screenWScale]);
    }
    [self.actLabel updateLayout];
    self.actNumberLabel.text = [NSString stringWithFormat:@"参与人数：%@人",model.ENROLL_NUM ? model.ENROLL_NUM :@"0"];
    
    if (self.isNoShow) {
        self.statusLabel.hidden = YES;
        self.addressLabel.sd_layout.rightSpaceToView(self, 10 * [UIScreen zj_screenWScale]);
        [self.addressLabel updateLayout];
    }else{
        /**  先判断活动是否结束，没结束判断是否报名，如果报名就显示请假，不然就是加入  */
        if ([model.IS_END isEqualToString:@"3"]) {
            self.statusLabel.hidden = YES;
        }else{
            self.statusLabel.hidden = NO;
            if ([model.IS_ENROLL isEqualToString:@"1"]) {
                if ([model.SIGN isEqualToString:@"1"]) {
                    self.statusLabel.text = @"已签到";
                }else{
                    if ([self.model.IS_LEAVE isEqualToString:@"0"]) {
                        self.statusLabel.text = @"请假";
                        
                    }else{
                        if ([model.LEAVE_STATE isEqualToString:@"0"]) {
                            self.statusLabel.text = @"请假待审核";
                        }else if ([model.LEAVE_STATE isEqualToString:@"1"]){
                            self.statusLabel.text = @"已请假";
                        }else{
                            self.statusLabel.text = @"请假";
                        }
                        
                    }
                }
                
            }else{
                self.statusLabel.text = @"加入";
            }
            
        
        }
        
        
    }
    
    CGFloat statusWidth = [CalculateLabelHeight CalculateLabelWidthWithString:self.statusLabel.text font:14 * [UIScreen zj_textFontScale] height:27 * [UIScreen zj_screenHScale]].width;
    if (statusWidth > 50 * [UIScreen zj_screenWScale]) {
        self.statusLabel.sd_layout.widthIs(statusWidth + 6 * [UIScreen zj_screenWScale]);
        [self.statusLabel updateLayout];

    }
    
    
    
    /**  判断活动是否在审核,只针对书记工作台  */
    if (self.isFromSecretaryRoom) {
        if ([model.AUDITING isEqualToString:@"0"]) {
            self.checkLabel.hidden = NO;
            self.statusLabel.hidden = YES;
        }else{
            self.checkLabel.hidden = YES;
        }
    }else{
        self.checkLabel.hidden = YES;
    }

    
    self.typeLabel.text = model.TAGS_NAMES;
}




- (void)tapLabel{
    if (self.delegate) {
        [self.delegate ActivityJoinClick_index:self.index section:self.section statusText:self.statusLabel.text];
    }
}


#pragma mark - getter
- (UIImageView *)addressImageView{
    if (!_addressImageView) {
        _addressImageView = [UIImageView new];
        _addressImageView.contentMode = UIViewContentModeCenter;
        _addressImageView.image = [UIImage imageNamed:@"hdxq_address"];
    }
    return _addressImageView;
}


- (UIImageView *)timeImageVIew{
    if (!_timeImageVIew) {
        _timeImageVIew = [UIImageView new];
        _timeImageVIew.contentMode = UIViewContentModeCenter;
        _timeImageVIew.image = [UIImage imageNamed:@"hdxq_time"];
    }
    return _timeImageVIew;
}
- (UIImageView *)actImageView{
    if (!_actImageView) {
        _actImageView = [UIImageView new];
        _actImageView.contentMode = UIViewContentModeCenter;
        _actImageView.clipsToBounds = YES;
    }
    return _actImageView;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor lightGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _timeLabel;
}

- (UILabel *)actNumberLabel{
    if (!_actNumberLabel) {
        _actNumberLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor themeColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:13 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _actNumberLabel;
}

- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor zj_colorWithR:20 G:20 B:20 Al:0.3];
            label.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _typeLabel;
}



- (UILabel *)statusLabel{
    if (!_statusLabel) {
        _statusLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor themeColor];
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            label.layer.cornerRadius = 5;
            label.layer.masksToBounds = YES;
            label.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel)];
            [label addGestureRecognizer:tap];
            
            label;
        });
    }
    return _statusLabel;
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor lightGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _addressLabel;
}

- (UILabel *)actLabel{
    if (!_actLabel) {
        _actLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.numberOfLines = 0;
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
            label.characterSpace = 1;
            label.lineSpace = 4 * [UIScreen zj_screenHScale];
            label;
        });
    }
    return _actLabel;
}



- (UILabel *)checkLabel{
    if (!_checkLabel) {
        _checkLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.numberOfLines = 2;
            label.text = @"待审";
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor themeColor];
            label.font = [UIFont systemFontOfSize:13 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _checkLabel;
}

@end
